//
//  FirstViewController.m
//  UI
//
//  Created by apple on 2017/7/3.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "FirstViewController.h"
#import "ViewController.h"
#import <CTAssetsPickerController/CTAssetsPickerController.h>
@interface FirstViewController ()<CTAssetsPickerControllerDelegate>

@property (nonatomic, strong) NSMutableArray *mutArrCTA;

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.mutArrCTA = [NSMutableArray arrayWithCapacity:0];
    self.mutArrCTA1 = [NSMutableArray arrayWithCapacity:0];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 50, 50)];
    
    [btn addTarget:self action:@selector(Clicl) forControlEvents:UIControlEventTouchUpInside];
    
    btn.backgroundColor = [UIColor blueColor];
    
//    [self.view addSubview:btn];
    
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
       
        CTAssetsPickerController *picker = [[CTAssetsPickerController alloc] init];
        
        picker.delegate = self;
        
        picker.showsEmptyAlbums = YES;
        picker.showsCancelButton = YES;
        picker.doneButtonTitle = @"完成";
        
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            
            picker.modalPresentationStyle = UIModalPresentationFormSheet;
        }
        
        [self presentViewController:picker animated:YES completion:nil];
    }];
    
}

- (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets{

//    [picker.navigationController pushViewController:[[ViewController alloc]init] animated:YES];
//    [picker presentViewController:[[ViewController alloc] init] animated:YES completion:nil];
    
    //关闭图片选择界面
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    //可以获取所有选中的图片
    //    NSLog(@"%@",assets);
    
    ViewController *vc = [[ViewController alloc] init];
    
    //获取屏幕像素与点坐标的比值
    CGFloat scale = [UIScreen mainScreen].scale;
    
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.resizeMode = PHImageRequestOptionsResizeModeExact;
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    
    //遍历选择的所有图片
    for (NSInteger i = 0; i < assets.count; i++) {
        
        PHAsset *asset = assets[i];
        
        //计算图片的尺寸
        CGSize size = CGSizeMake(asset.pixelWidth / scale, asset.pixelHeight / scale);
        
        [self.mutArrCTA addObject:asset];
        vc.mutArrV = _mutArrCTA;
        vc.sizeA = size;
        
        //获取图片
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            
            //获取图片成功就会来到这个block中
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.image = result;
//            [self.mutArrCTA addObject:result];
            
//            vc.mutArrV = _mutArrCTA;
            
//            [self presentViewController:vc animated:YES completion:nil];
//            imageView.frame = CGRectMake(10, 100, 300, 320);
//            [self.view addSubview:imageView];
        }];
    }
    
    [self presentViewController:vc animated:YES completion:nil];

}

-(void)assetsPickerController:(CTAssetsPickerController *)picker didSelectAsset:(PHAsset *)asset{

    NSLog(@"%@",asset);
}

- (void)Clicl{

    [self.navigationController pushViewController:[[ViewController alloc]init] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
